package org.b3log;

import static org.b3log.common.Constants.*;

import org.b3log.task.LoadChannelTask;
import org.b3log.view.Dialog;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class HomeActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		initButton((Button) findViewById(R.id.btnPhoto), 0);
		initButton((Button) findViewById(R.id.btnModel), 1);
		initButton((Button) findViewById(R.id.btnDesign), 2);
		initButton((Button) findViewById(R.id.btnActor), 3);
		initButton((Button) findViewById(R.id.btnMusic), 4);
		initButton((Button) findViewById(R.id.btnMovie), 5);
		initButton((Button) findViewById(R.id.btnMedia), 6);
		initButton((Button) findViewById(R.id.btnArt), 7);
	}

	public void initButton(Button button, int index) {
		button.setTag(index);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				loadChannel((Integer) v.getTag());
			}
		});
	}

	public void loadChannel(int index) {
		LoadChannelTask task = new LoadChannelTask(HomeActivity.this);
		task.execute(DOMAIN + CHANNEL + CHANNELS_ENGLISH[index], CHANNELS_CHINESE[index]);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_home, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_login:
			Toast.makeText(HomeActivity.this, R.string.developing, 1000).show();
			// do sth
			break;
		case R.id.menu_feedback:
			Toast.makeText(HomeActivity.this, R.string.developing, 1000).show();
			// do sth
			break;
		case R.id.menu_about:
			Intent intent = new Intent();
			intent.setClass(this, AboutActivity.class);
			startActivity(intent);
			break;
		case R.id.menu_exit:
			Dialog.getInstance().exitConfirmDialog(HomeActivity.this);
			break;
		}
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Dialog.getInstance().exitConfirmDialog(HomeActivity.this);
		}
		return super.onKeyDown(keyCode, event);
	}

}